<template>
  <div class="box">
    <!-- <el-breadcrumb separator="/" style="margin-bottom: 10px">
          <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
          <el-breadcrumb-item>文章管理</el-breadcrumb-item>
          <el-breadcrumb-item>文章分类</el-breadcrumb-item>
        </el-breadcrumb> -->
    <el-card>
      <div class="cate-header">
        <Bread first="文章管理" second="文章分类"></Bread>
        <el-button @click="addCate" type="primary">添加</el-button>
      </div>

      <!-- 表格 -->
      <el-table border :data="artlist" style="width: 100%; margin: 20px 0">
        <el-table-column
          label="序号"
          width="180"
          type="index"
        ></el-table-column>

        <el-table-column label="分类名称" prop="cate_name"></el-table-column>

        <el-table-column label="分类别名" prop="cate_alias"></el-table-column>

        <el-table-column label="操作">
          <template slot-scope="scope">
            <el-button
              type="primary"
              size="mini"
              @click="handleEdit(scope.row.id)"
              >编辑</el-button
            >
            <!-- 编辑对话框 -->
            <el-dialog
              title="编辑文章分类"
              :visible.sync="diaEditVisible"
              width="35%"
              @close="close1"
            >
              <el-form ref="editforms" :model="editform" :rules="addRules">
                <el-form-item
                  label="分类名称"
                  label-width="90px"
                  prop="cate_name"
                >
                  <el-input
                    v-model="editform.cate_name"
                    autocomplete="off"
                  ></el-input>
                </el-form-item>
                <el-form-item
                  label="分类别名"
                  label-width="90px"
                  prop="cate_alias"
                >
                  <el-input
                    v-model="editform.cate_alias"
                    autocomplete="off"
                  ></el-input>
                </el-form-item>
              </el-form>
              <span slot="footer" class="dialog-footer">
                <el-button @click="diaEditVisible = false">取 消</el-button>
                <el-button type="primary" @click="uptateHandle"
                  >确 定</el-button
                >
              </span>
            </el-dialog>
            <el-button
              size="mini"
              type="danger"
              @click="handleDelete(scope.row.id)"
              >删除</el-button
            >
          </template>
        </el-table-column>
      </el-table>
    </el-card>
    <!-- 添加对话框 -->
    <el-dialog
      title="添加文章分类"
      :visible.sync="dialogVisible"
      width="35%"
      @close="close"
    >
      <el-form ref="addform" :model="form" :rules="addRules">
        <el-form-item label="分类名称" label-width="90px" prop="cate_name">
          <el-input v-model="form.cate_name" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="分类别名" label-width="90px" prop="cate_alias">
          <el-input v-model="form.cate_alias" autocomplete="off"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="addHandle">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>
<script>
import { mapState, mapActions } from 'vuex'
import {
  addcateApi,
  delcateApi,
  editcateApi,
  updatecateApi
} from '../../api/art'
export default {
  name: '',
  data () {
    return {
      // 添加
      dialogVisible: false,
      form: {
        cate_name: '',
        cate_alias: ''
      },
      // 编辑
      diaEditVisible: false,
      editform: {
        id: '',
        cate_name: '',
        cate_alias: ''
      },
      // 校验规则
      addRules: {
        cate_name: [
          { required: true, message: '请输入分类名称', trigger: 'blur' },
          {
            pattern: /^\S{1,10}$/,
            message: '分类名称必须是1-10位的非空字符串',
            trigger: 'blur'
          }
        ],
        cate_alias: [
          { required: true, message: '请输入分类别名', trigger: 'blur' },
          {
            pattern: /^[a-zA-Z0-9]{1,15}$/,
            message: '分类别名必须是1-15位的字母数字',
            trigger: 'blur'
          }
        ]
      }
    }
  },
  created () {
    this.AfgetArtList()
  },
  computed: {
    ...mapState('article', ['artlist'])
  },
  methods: {
    ...mapActions('article', ['AfgetArtList']),
    // 编辑按钮
    async handleEdit (id) {
      console.log(id)
      // 通过id发送请求
      const { data } = await editcateApi(id)
      console.log(data, 236)
      // if (res.code !== 0) return this.$message.error('获取分类列表失败')
      // 将获取到的数据显示在对话框里
      this.editform = data.data
      // 点击编辑按钮 显示编辑对话框
      this.diaEditVisible = true
    },
    // 删除按钮
    async handleDelete (id) {
      const res = await this.$confirm('确定要删除这条数据?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).catch((err) => err)
      // console.log(res, 8989)
      const { data } = await delcateApi(id)
      // if (data.code === 0) {
      //   console.log(data, 987)
      //   this.AfgetArtList()
      // }
      console.log(data, 987)
      this.AfgetArtList()
      if (res === 'confirm') {
        this.$message({
          type: 'success',
          message: '删除成功'
        })
      }
    },
    // 添加按钮
    addCate () {
      /*
      1、显示对话框
      2、成功后数据列表更新
      .
       */
      this.dialogVisible = true
    },
    // 重置添加对话框
    close () {
      this.$refs.addform.resetFields()
    },
    // 重置编辑对话框
    close1 () {
      this.$refs.editforms.resetFields()
    },
    // 确认添加按钮
    addHandle () {
      /*
      1、预校验
      2、发送请求
      3、刷新页面
      4-关闭对话框
       */
      this.$refs.addform.validate(async (valid) => {
        //  校验成功
        // 2、发送请求 api
        const res = await addcateApi(this.form)
        if (res.data.code === 0) {
          this.$message({
            type: 'success',
            message: '添加成功'
          })
          // 添加成功 刷新页面
          this.AfgetArtList()
          // 4-关闭对话框
          this.dialogVisible = false
        }
      })
    },
    // 编辑更新
    uptateHandle () {
      /*
      1、预校验
      2、发送请求
      3、刷新页面
      4-关闭对话框
       */
      this.$refs.editforms.validate(async (valid) => {
        //  校验成功
        // 2、发送请求 api
        const res = await updatecateApi(this.editform)
        if (res.data.code === 0) {
          this.$message({
            type: 'success',
            message: '编辑成功'
          })
          // 添加成功 刷新页面
          this.AfgetArtList()
          // 4-关闭对话框
          this.diaEditVisible = false
        }
      })
    }
  }
}
</script>
<style lang='less'  scoped>
.box {
  .cate-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
  }
}
</style>
